package com.gable.exhibition.service.impl;

import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.*;
import com.gable.exhibition.model.Comment;
import com.gable.exhibition.model.Repair;
import com.gable.exhibition.model.Upkeep;
import com.gable.exhibition.service.CommentService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.CommentInfoView;
import com.gable.exhibition.view.CommentView;
import com.gable.exhibition.view.InstallView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by SJX on 2018/3/7.
 */
@Service("commentService")
public class CommentServiceImpl implements CommentService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    CommentDao commentDao;

    @Autowired
    CommentInfoDao commentInfoDao;

    @Autowired
    RepairDao repairDao;

    @Autowired
    UpkeepDao upkeepDao;

    @Autowired
    InstallDao installDao;

    @Override
    public ReturnResultUtil queryComment(CommentView commentView) {
        List<CommentView> list = commentDao.queryComment(commentView);
        if(list!=null && !list.isEmpty()){
            CommentView cv = list.get(1);
            return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,cv);
        }
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,new CommentView());
    }
    @Override
    public ReturnResultUtil queryReplyComment(CommentView commentView) {
        List<CommentView> list = commentDao.queryReplyComment(commentView);
        if(list!=null && !list.isEmpty()){
            Comment cv = list.get(0);
            return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,cv);
        }
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,new CommentView());
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addComment(CommentView commentView) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        commentView.setCommentId(UUIDGenerator.getUniqueLong());
        commentView.setCreateTime(df.format(new Date()));
        commentView.setIsDel(0);
        commentDao.insertSelective(commentView);

        //判断如果是回复评论，要更新维修评论状态
        if(commentView.getParentId()!=null){
            if(commentView.getKeyType()==2901L){//维修
                Repair repair = new Repair();
                repair.setRepairId(commentView.getKeyId());
                repair.setCommentStatus(3503L);
                repairDao.updateByPrimaryKeySelective(repair);
            }else if(commentView.getKeyType()==2902L){//保养
                Upkeep upkeep = new Upkeep();
                upkeep.setUpkeepId(commentView.getKeyId());
                upkeep.setCommentStatus(3503L);
                upkeepDao.updateByPrimaryKeySelective(upkeep);
            }else if(commentView.getKeyType()==2903L){//安装
                InstallView install = new InstallView();
                install.setInstallId(commentView.getKeyId());
                install.setCommentStatus(3503L);
                installDao.updateByPrimaryKeySelective(install);
            }
        }

        List<CommentInfoView> infoViews = commentView.getCommentInfoList();
        if(infoViews!=null && !infoViews.isEmpty()){
            for(CommentInfoView infoView:infoViews){
                infoView.setCommentInfoId(UUIDGenerator.getUniqueLong());
                infoView.setCommentId(commentView.getCommentId());
                infoView.setCreateTime(df.format(new Date()));
                infoView.setIsDel(0);
                commentInfoDao.insertSelective(infoView);
            }
        }
        return new ReturnResultUtil(200, MessageUtil.SAVESUCCESS,commentView);
    }
}
